<?php


namespace catchAdmin\business\controller;


use app\Request;
use catchAdmin\business\model\Airlift as OrderModel;
use catcher\CatchAuth;
use catcher\CatchResponse;

class Index
{
    protected $orderModel;

    /**
     *
     * @time 2021/11/03 14:02
     * @param OrderModel $orderModel
     * @return mixed
     */
    public function __construct(OrderModel $orderModel)
    {
        $this->orderModel = $orderModel;
    }

    /**
     * 订单列表
     *
     * @time 2022年03月28日
     * @param CatchRequest $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function statistics(CatchAuth $auth, Request $request)
    {
        $user = $auth->user();
        $baseData = [];$lineChartData=[];$barChartData=[];
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
        $days = date("t");
        $beginThismonth = mktime(0,0,0,date('m'),1,date('Y'));
        $endThismonth = mktime(23,59,59,date('m'),date('t'),date('Y'));
        //非管理员类型需要带商户号
        if($user['type'] !== 1){
            $where = [];
            $where[] = ['created_at','between',[$beginThismonth,$endThismonth]];
            $where[] = ['business_type','in',['AE','AI']];
            $where[] = ['tenant_id','=',$user['tenant_id']];
            $orders = $this->orderModel->where($where)->order('created_at asc')->select()->toArray();
            $where = [];
            $where[] = ['business_type','in',['AE','AI']];
            $where[] = ['tenant_id','=',$user['tenant_id']];
            $baseData['totalOrders'] = $this->orderModel->where($where)->count();
            $where = [];
            $where[] = ['created_at','between',[$beginYesterday,$endYesterday]];
            $where[] = ['business_type','in',['AE','AI']];
            $where[] = ['tenant_id','=',$user['tenant_id']];
            $baseData['yesterdayOrder'] = $this->orderModel->where($where)->count();
            $where = [];
            $where[] = ['created_at','between',[$beginThismonth,$endThismonth]];
            $where[] = ['business_type','in',['SE','SI']];
            $where[] = ['tenant_id','=',$user['tenant_id']];
            $clears = $this->orderModel->where($where)->order('created_at asc')->select()->toArray();
            $where = [];
            $where[] = ['business_type','in',['SE','SI']];
            $where[] = ['tenant_id','=',$user['tenant_id']];
            $baseData['totalClears'] = $this->orderModel->where($where)->count();
            $where = [];
            $where[] = ['created_at','between',[$beginYesterday,$endYesterday]];
            $where[] = ['business_type','in',['SE','SI']];
            $where[] = ['tenant_id','=',$user['tenant_id']];
            $baseData['yesterdayClear'] = $this->orderModel->where($where)->count();
        }else{
            $where = [];
            $where[] = ['created_at','between',[$beginThismonth,$endThismonth]];
            $where[] = ['business_type','in',['AE','AI']];
            $orders = $this->orderModel->where($where)->order('created_at asc')->select()->toArray();
            $where = [];
            $where[] = ['business_type','in',['AE','AI']];
            $baseData['totalOrders'] = $this->orderModel->where($where)->count();
            $where = [];
            $where[] = ['created_at','between',[$beginYesterday,$endYesterday]];
            $where[] = ['business_type','in',['AE','AI']];
            $baseData['yesterdayOrder'] = $this->orderModel->where($where)->count();
            $where = [];
            $where[] = ['created_at','between',[$beginThismonth,$endThismonth]];
            $where[] = ['business_type','in',['SE','SI']];
            $clears = $this->orderModel->where($where)->order('created_at asc')->select()->toArray();
            $where = [];
            $where[] = ['business_type','in',['SE','SI']];
            $baseData['totalClears'] = $this->orderModel->where($where)->count();
            $where = [];
            $where[] = ['created_at','between',[$beginYesterday,$endYesterday]];
            $where[] = ['business_type','in',['SE','SI']];
            $baseData['yesterdayClear'] = $this->orderModel->where($where)->count();
        }
        $baseData['toMonthOrder'] = count($orders);//本月订单
        $baseData['toMonthClear'] = count($clears);//本月清单
        //年度统计
        $month_now=date('n');
        $year_now=date('Y');
        for($i=0;$i<12;$i++){
            if($i+1<10){
                $month = $year_now.'-0'.($i+1);
                $barChartData['times'][] = $month;
            }else{
                $month = $year_now.'-'.($i+1);
                $barChartData['times'][] = $month;
            }
            $d = date('t', strtotime($year_now . '-' . ($i+1)));
            $between = [strtotime($month),mktime(23, 59, 59, ($i+1), $d, $year_now)];
            if($i<$month_now){
                if($user['type'] !== 1) {
                    $where = [];
                    $where[] = ['created_at', 'between', $between];
                    $where[] = ['business_type', 'in', ['AE', 'AI']];
                    $where[] = ['tenant_id', '=', $user['tenant_id']];
                    $barChartData['orders'][] = $this->orderModel->where($where)->count();
                    $where = [];
                    $where[] = ['created_at','between',$between];
                    $where[] = ['business_type','in',['SE','SI']];
                    $where[] = ['tenant_id','=',$user['tenant_id']];
                    $barChartData['clears'][] = $this->orderModel->where($where)->count();
                }else{
                    $where = [];
                    $where[] = ['created_at', 'between', $between];
                    $where[] = ['business_type', 'in', ['AE', 'AI']];
                    $barChartData['orders'][] = $this->orderModel->where($where)->count();
                    $where = [];
                    $where[] = ['created_at','between',$between];
                    $where[] = ['business_type','in',['SE','SI']];
                    $barChartData['clears'][] = $this->orderModel->where($where)->count();
                }
            }
        }

        $chartData = [];
        for ($i = 1; $i <= $days; $i++){
            $chartData[$i.'日']['orders'] = 0;
            $chartData[$i.'日']['clears'] = 0;
            $day = $i>=10 ? $i : "0".$i;
            $date = date('Y-m').'-'.$day;
            $start = strtotime($date);
            $start = date('Y-m-d H:i:s',$start);
            $end = strtotime($date)+3600*24-1;
            $end = date('Y-m-d H:i:s',$end);
            foreach ($orders as $key => $value){
                if($value['created_at']>=$start && $value['created_at']<=$end){
                    $chartData[$i.'日']['orders']++;
                    unset($orders[$key]);
                }else{
                    break 1;
                }
            }
            foreach ($clears as $key => $value){
                if($value['created_at']>=$start && $value['created_at']<=$end){
                    $chartData[$i.'日']['clears']++;
                    unset($clears[$key]);
                }else{
                    break 1;
                }
            }
        }
        $baseData['todayOrder'] = $chartData[(int)date('d').'日']['orders'];//本日订单
        $baseData['todayClear'] = $chartData[(int)date('d').'日']['clears'];//本日清单
        $lineChartData['times'] = array_keys($chartData);
        $lineChartData['orders'] = array_column($chartData,'orders');
        $lineChartData['clears'] = array_column($chartData,'clears');
        $data['baseData'] = $baseData;
        $data['lineChartData'] = $lineChartData;
        $data['barChartData'] = $barChartData;
        return CatchResponse::success($data, '成功');
    }
}